package com.aweqy.subjectredis.controller;

import com.aweqy.subjectredis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis")
public class RedisController {
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @PostMapping("/set")
    public String setValue(@RequestParam String key, @RequestParam String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return "设置成功";
    }
    
    @GetMapping("/get")
    public String getValue(@RequestParam String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
    
    @PostMapping("/setObject")
    public String setObject(@RequestParam String key, @RequestBody User user) {
        redisTemplate.opsForValue().set(key, user);
        return "设置对象成功";
    }
    
    @GetMapping("/getObject")
    public User getObject(@RequestParam String key) {
        return (User) redisTemplate.opsForValue().get(key);
    }
}